#ifndef UDPPARSER_H
#define UDPPARSER_H

#include <QList>
#include <QDebug>
#include <QMetaType>

typedef struct udpInfo{
    char header[8];
    int dataCount;
    unsigned long dateTime;
    unsigned int colCode;
    unsigned int date[8];
    double latitude;
    double longitude;
    double altitude;
    double utm1;
    double utm2;
    int markIndex[4];
    double cx1;
    double cx2;
    double cx21;
    double flag[3];
//        bool check;
//        char tail[8];
    friend QDebug operator << (QDebug os ,udpInfo info)
       {
           os << "latitude: " << info.latitude << "lon/alt: " << info.longitude << "/" << info.altitude
              << "utm1/utm2" << info.utm1 << "/" << info.utm2
              << "cx1/cx2/cx21: " << info.cx1 << "/" << info.cx2 << "/" << info.cx21;
           return os;
       }
}udpInfo;

    Q_DECLARE_METATYPE(udpInfo)


class UDPParser
{
public:
    UDPParser();
    ~UDPParser();

    enum UDPStructure{
        SINGLE_PACK_SIZE = 169,
        SINGLE_DATA_SIZE = 148,

        SEEK_HEADER_SIZE = 8,
        SEEK_DATA_COUNT_SIZE = SEEK_HEADER_SIZE + 4,
        SEEK_DATA_SIZE = SEEK_DATA_COUNT_SIZE + SINGLE_DATA_SIZE,
        SEEK_CHECKSUM_SIZE = SEEK_DATA_SIZE + 1,

        SEEK_DATA_TIMER_SIZE = 8,
        SEEK_DATA_COL_SIZE = 4+8,
        SEEK_DATETIME_SIZE = 4+8+32,
        SEEK_LAT_LON_SIZE = 4+8+32+24,
        SEEK_UTM_SIZE = 4+8+32+24+16,
        SEEK_MARK_SIZE = 4+8+32+24+16+16,
        SEEK_CX1_SIZE = SEEK_MARK_SIZE + 8,
        SEEK_CX2_SIZE = SEEK_CX1_SIZE + 8,
        SEEK_CX21_SIZE = SEEK_CX2_SIZE + 8,
    };



    udpInfo *currUdpData;
//    int parseSinglePackage(QByteArray singleArr);
//    int parseSingleData(QByteArray singleData);

    int parseSinglePackage(QByteArray singleArr);
    //For test
    void readFile();

private:
    void initData();
    const QByteArray header = "HRMAGAIR";
    const QByteArray tail = "RIAGAMRH";

};

QByteArray getSegment(QByteArray buff, int begin, int end);
#endif // UDPPARSER_H
